(function(){
    //获取图片路径方法
    function getObjectURL(file) {
        var url = null ;
        if (window.createObjectURL!=undefined) { // basic
            url = window.createObjectURL(file) ;
        } else if (window.URL!=undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file) ;
        } else if (window.webkitURL!=undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file) ;
        }
        return url ;
    }

    function preview(that,options) {
        //接受files数组列表
        var _file = that.files,str = "";
        //限制上传图片的最大值
        if(_file.length > options.maxLen){
            alert("最多上传" + options.maxLen + "张图片！");
            return;
        }

        //循环拼接为字符串
        for(var i = 0; i < _file.length; i++){
            str += "<img src='" + getObjectURL(_file[i]) + "'/>";
        }

        //将图片动态添加到图片展示区
        document.getElementById(options.showID).innerHTML = str;
    }


    var upload_preview = window.uploadPreview = function(options){
        var defaults = {
            fileID : options.fileID || "file",
            showID : options.showID || "img-box",
            maxLen : options.maxLen || 3
        }
        //获取页面的input标签
        var file = document.getElementById(defaults.fileID);
        //给input绑定change事件
        file.onchange = function(){
            var that = this;
            var len = that.files.length;
            if (len == 0) {
                that.nextElementSibling.value = '';
                that.nextElementSibling.nextElementSibling.innerHTML = '';
                console.log('点了取消按钮，清空file控件内容和图片Id');
                return;
            } else {
                var fileAccaccept = that.getAttribute('accaccept');
                var fileType = that.files[0].type;
                var type = (fileType.substr(fileType.lastIndexOf("/") + 1)).toLowerCase();

                if (!type || fileAccaccept.indexOf(type) == -1) {
                    lightyear.notify('您上传图片的类型不符合(' + fileAccaccept + ')', 'danger', 1000);
                    that.value = '';
                    console.log('选择的文件不符合类型，清空file控件的内容');
                    if (that.nextElementSibling.value == '') {
                        that.nextElementSibling.nextElementSibling.innerHTML = '';
                        console.log('图片Id也为空时，清空显示图片');
                    }
                    return;
                } else {
                    that.nextElementSibling.value = '';
                    preview(that,defaults);
                }
            }
        };
    }
})()